from socket import *
"""http 1.0"""

class TcpServer:
    def __init__(self,str_find,ip="127.0.0.1",port=1345):
        self.__socket=socket(AF_INET,SOCK_STREAM)
        self.__socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
        self.ip=ip
        self.port=port
        self.__socket.bind((ip,port))
        self.__socket.listen(15)
        self.__find=str_find

    def __accept(self):
        print("waiting for connection...")
        print(f"start homepage in:http://{self.ip}:{self.port}{self.__find} click to visit")
        __conn,__addr=self.__socket.accept()
        return __conn

    def response(self,number=1024):
        conn=self.__accept()
        data=conn.recv(number)
        if not data:
            return
        info=data.decode().split()[1]
        abnormal_request="""HTTP/1.1 404 NOT FOUND\r\nContent-Type: text/html\r\n
        <h1>404 Not Found</h1>"""
        normal_request="""HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\nNormal_request"""
        if info==self.__find:
            conn.send(normal_request.encode())
            conn.close()
        else:
            conn.send(abnormal_request.encode())
            conn.close()

    def __call__(self,func):
        def wrapper(*args,**kwargs):
            self.response()
            return func(*args,**kwargs)
        return wrapper


if __name__=="__main__":
    @TcpServer("/cat")
    def get_one():
        print("hello world")
    get_one()